/*
	trans XFCN
	2 Mar 1989 Greg Jorgensen, Mac DTS
	й Apple Computer Inc., 1989
	
	usage: trans(src, dst, string)
	
	translates src to dst in string, returns translated string
	
	for example:
		trans("abc", "123", "axbycz") -> "1x2y3z"
		
	if dst is shorter than src, the matching characters are removed:
		trans("abc", "12", "axbycz") -> "1x2yz"
*/

#include <MacTypes.h>
#include <SetUpA4.h>
#include <strings.h>
#include <HyperXCmd.h>

/* Hypercard parameter block:

	typedef struct XCmdBlock {
		short	paramCount;     
	    Handle  params[16];
	    Handle  returnValue;      
	    Boolean	passFlag; 
	      
	    void	(*entryPoint)();
	    short	request;  
	    short	result;  
	    long	inArgs[8];
	    long	outArgs[4];
	   } XCmdBlock, *XCmdBlockPtr;
*/

/* еее */

pascal void main(paramPtr)
	XCmdBlockPtr	paramPtr;
{
	register	char	*s, *t, *src, *dst;
	int					i,j,m;
	
	RememberA0();					/* A0 points to this procedure */
	SetUpA4();						/* point A4 at this proc for literals */
	
	if (paramPtr->paramCount != 3)	/* must have 3 params */
		return;
	
	HLock(paramPtr->params[0]);
	src = *(paramPtr->params[0]);
	HLock(paramPtr->params[1]);
	dst = *(paramPtr->params[1]);
	HLock(paramPtr->params[2]);
	s = *(paramPtr->params[2]);
	
	paramPtr->returnValue = NewHandle( strlen(s) );
	HLock(paramPtr->returnValue);
	t = *(paramPtr->returnValue);
	
	m = strlen(dst);
	for (i=0; s[i] != '\0'; i++) {
		for (j=0; ((src[j] != '\0') && (src[j] != s[i])); j++)
			;
		if (src[j] == '\0')			/* s[i] not in src */
			*t++ = s[i];
		else if (j < m)				/* s[i] in src[j]; dst[j] exists */
			*t++ = dst[j];
	}
	*t = '\0';
	
	HUnlock(paramPtr->params[0]);
	HUnlock(paramPtr->params[1]);
	HUnlock(paramPtr->params[2]);
	HUnlock(paramPtr->returnValue);
	RestoreA4();					/* point A4 back to where it was */
}

#include "XCmdGlue.inc.c"
